package com.leon.notepad.content;

import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.leon.notepad.R;
import com.leon.notepad.content.bean.NotepadBean;
import com.leon.notepad.content.utils.BarChartManager;
import com.leon.notepad.content.utils.DateUtils;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class StatisticalActivity extends BaseActivity {

    @BindView(R.id.bar_chat)
    BarChart barChat;
    @BindView(R.id.mPieChart)
    PieChart mPieChart;

    private long oneDay = 24 * 60 * 60 * 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_statistical);
        ButterKnife.bind(this);

        long today = DateUtils.getTodayTime();
        List<NotepadBean> day1 = LitePal.where("time > ?", today + "").find(NotepadBean.class);
        List<NotepadBean> day2 = LitePal.where("time > ? and time <= ?", today - oneDay + "", today + "").find(NotepadBean.class);
        List<NotepadBean> day3 = LitePal.where("time > ? and time <= ?", today - 2 * oneDay + "", today - oneDay + "").find(NotepadBean.class);

        showBarChartAlong(day1.size(), day2.size(), day3.size());
        showPieChart();
    }

    /**
     * 显条柱状图
     */
    private void showBarChartAlong(int day1, int day2, int day3) {
        BarChartManager barChartManager = new BarChartManager(barChat);

        List<BarEntry> yVals = new ArrayList<>();
        yVals.add(new BarEntry(1f, day1));
        yVals.add(new BarEntry(2f, day2));
        yVals.add(new BarEntry(3f, day3));
        String label = "";
        barChartManager.showBarChart(yVals, label, Color.parseColor("#0081ff"));
    }

    private void showPieChart(){
        mPieChart.setUsePercentValues(true);
        mPieChart.getDescription().setEnabled(false);
        mPieChart.setExtraOffsets(5, 10, 5, 5);

        mPieChart.setDragDecelerationFrictionCoef(0.95f);
        //设置中间文件
        mPieChart.setCenterText(new SpannableString("记事类型统计"));

        mPieChart.setDrawHoleEnabled(true);
        mPieChart.setHoleColor(Color.WHITE);

        mPieChart.setTransparentCircleColor(Color.WHITE);
        mPieChart.setTransparentCircleAlpha(110);

        mPieChart.setHoleRadius(58f);
        mPieChart.setTransparentCircleRadius(61f);

        mPieChart.setDrawCenterText(true);

        mPieChart.setRotationAngle(0);
        // 触摸旋转
        mPieChart.setRotationEnabled(true);
        mPieChart.setHighlightPerTapEnabled(true);

        List<NotepadBean> list = LitePal.where("type = ?", "0").find(NotepadBean.class);
        List<NotepadBean> list1 = LitePal.where("type = ?", "1").find(NotepadBean.class);

        ArrayList<PieEntry> entries = new ArrayList<PieEntry>();
        entries.add(new PieEntry(list.size(), "文字记事"));
        entries.add(new PieEntry(list1.size(), "混合记事"));

        //设置数据
        setData(entries);

        mPieChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);

        Legend l = mPieChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        l.setOrientation(Legend.LegendOrientation.VERTICAL);
        l.setDrawInside(false);
        l.setXEntrySpace(7f);
        l.setYEntrySpace(0f);
        l.setYOffset(0f);

        // 输入标签样式
        mPieChart.setEntryLabelColor(Color.WHITE);
        mPieChart.setEntryLabelTextSize(12f);
    }

    //设置数据
    private void setData(ArrayList<PieEntry> entries) {
        PieDataSet dataSet = new PieDataSet(entries, "");
        dataSet.setSliceSpace(3f);
        dataSet.setSelectionShift(5f);

        //数据和颜色
        ArrayList<Integer> colors = new ArrayList<Integer>();
        for (int c : ColorTemplate.VORDIPLOM_COLORS)
            colors.add(c);
        for (int c : ColorTemplate.JOYFUL_COLORS)
            colors.add(c);
        for (int c : ColorTemplate.COLORFUL_COLORS)
            colors.add(c);
        for (int c : ColorTemplate.LIBERTY_COLORS)
            colors.add(c);
        for (int c : ColorTemplate.PASTEL_COLORS)
            colors.add(c);
        colors.add(ColorTemplate.getHoloBlue());
        dataSet.setColors(colors);
        PieData data = new PieData(dataSet);
        data.setValueFormatter(new PercentFormatter());
        data.setValueTextSize(11f);
        data.setValueTextColor(Color.WHITE);
        mPieChart.setData(data);
        mPieChart.highlightValues(null);
        //刷新
        mPieChart.invalidate();
    }

}